package no.ntnu.idi.deid.patternmatching.search.dictionary.phrase;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import no.ntnu.idi.deid.config.Constants;
import no.ntnu.idi.deid.config.FilePaths;
import no.ntnu.idi.deid.config.OperatingSystem;

public class Simstring {

	private Simstring() {
		
	}
	
	private static String getExecuteString(File databasefile, File outputFile, double threshold, String searchString) {
		if(Constants.OPERATING_SYSTEM == OperatingSystem.LINUX) {
			return "python " + FilePaths.getSimstringQueryScript()
					+ " " + databasefile.getPath() 
					+ " " + outputFile.getPath()
					+ " " + threshold
					+ " " + searchString;
		}
		else {
			String stringThreshold = String.valueOf(threshold);
			stringThreshold = stringThreshold.replaceAll("\\.", ",");
			return FilePaths.getSimstringQueryWindows()
					+ " " + databasefile.getPath() 
					+ " " + outputFile.getPath()
					+ " " + stringThreshold
					+ " " + searchString;

		}
	}
	
	public static List<String> simStringQuery(File databasefile, File outputFile, double threshold, String searchString) {
		
		if (databasefile.isFile()) {
			try {
				Runtime r = Runtime.getRuntime();
				Process p = r.exec(getExecuteString(databasefile,outputFile,threshold,searchString));
				p.waitFor();
			} catch (Exception e) {
				String cause = e.getMessage();
				if (cause.equals("python: not found"))
					System.out.println("No python interpreter found.");
				return Collections.emptyList();
			}
		}
		List<String> result = getResult(outputFile);
//		outputFile.delete();
		return result;
	}
	
	@SuppressWarnings("resource")
	private static List<String> getResult(File outputFile) {
		ArrayList<String> result = new ArrayList<String>();
		BufferedReader br = null;
		FileInputStream in = null;
		try {
			in = new FileInputStream(outputFile);
			br = new BufferedReader(new InputStreamReader(in));
			  String strLine;
			 
			  while((strLine = br.readLine())!= null)
			  {
				if(strLine.equals("null")) {
					return Collections.emptyList();
					}
					else if(strLine.isEmpty()) { 
						//do nothing
					}
					else {
						result.add(strLine);
					}
			  }
			  br.close();
		} catch (IOException e) {
			if(br != null) {
				try {
					br.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	
	public static List<String> simStringQuery(String databasefile, String outputFile, double threshold, String searchString) {
		return simStringQuery(new File(databasefile), new File(outputFile), threshold, searchString);
	}
	
	private static String getDatabaseString(File inputTextFile, File outputDatabaseFile) {
		if(Constants.OPERATING_SYSTEM == OperatingSystem.LINUX) {
			return "python " + FilePaths.getSimStringDatabaseBuilderScript()
					+ " " + inputTextFile.getPath() 
					+ " " + outputDatabaseFile.getPath();
		}
		else {
			return FilePaths.getSimstringDataBaseBuilderWindows()
					+ " " + inputTextFile.getPath() 
					+ " " + outputDatabaseFile.getPath();

		}
	}
	
	public static File buildSimstringDatabase(File inputTextFile, File outputDatabaseFile) {
		if (inputTextFile.isFile()) {
			try {
				Runtime r = Runtime.getRuntime();
				Process p = r.exec(getDatabaseString(inputTextFile,outputDatabaseFile));
				p.waitFor();
			} catch (Exception e) {
				String cause = e.getMessage();
				if (cause.equals("python: not found"))
					System.out.println("No python interpreter found.");
			}
		}
		return outputDatabaseFile;
	}
	
	public static File buildSimstringDatabase(String inputTextFile, String outputDatabaseFile) {
		return buildSimstringDatabase(new File(inputTextFile), new File(outputDatabaseFile));
	}
	
}

